package com.xintai.system.controller;

import java.util.List;

import com.xintai.utils.StaticString;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.system.domain.ErpClient;
import com.xintai.system.service.IErpClientService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author xintai
 * @date 2021-01-12
 */
@RestController
@RequestMapping("/system/client")
public class ErpClientController extends BaseController
{
    @Autowired
    private IErpClientService erpClientService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:client:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpClient erpClient)
    {
        startPage();
        List<ErpClient> list = erpClientService.selectErpClientList(erpClient);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:client:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ErpClient erpClient)
    {
        List<ErpClient> list = erpClientService.selectErpClientList(erpClient);
        ExcelUtil<ErpClient> util = new ExcelUtil<ErpClient>(ErpClient.class);
        return util.exportExcel(list, "client");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:client:query')")
    @GetMapping(value = "/{cId}")
    public AjaxResult getInfo(@PathVariable("cId") Integer cId)
    {
        return AjaxResult.success(erpClientService.selectErpClientById(cId));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('system:client:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpClient erpClient)
    {
        if (erpClientService.verifyErpClientName(erpClient))
            return AjaxResult.error(StaticString.NameRepeat(erpClient.getcName()));
        return toAjax(erpClientService.insertErpClient(erpClient));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('system:client:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpClient erpClient)
    {
        if (erpClientService.verifyErpClientName(erpClient))
            return AjaxResult.error(StaticString.NameRepeat(erpClient.getcName()));
        return toAjax(erpClientService.updateErpClient(erpClient));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('system:client:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cIds}")
    public AjaxResult remove(@PathVariable Integer[] cIds)
    {
        return toAjax(erpClientService.deleteErpClientByIds(cIds));
    }
}
